package org.xml.create.performanceTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

public class performanceTest {
	private static final int NUMBER_OF_TEST = 10;

	@Test
	public void testperFormance() throws ParserConfigurationException,
			TransformerFactoryConfigurationError, TransformerException,
			SAXException, IOException {

		System.out.println("============== 四种生成XML文件的性能测试（测试10次，取平均值） =============");
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < NUMBER_OF_TEST; i++) {
			newXMLByDOM();
		}
		System.out.println("DOM方式耗时：\t"
				+ (System.currentTimeMillis() - startTime) / NUMBER_OF_TEST
				+ "毫秒");

		startTime = System.currentTimeMillis();
		for (int i = 0; i < NUMBER_OF_TEST; i++) {
			newXMLBySAX();
		}
		System.out.println("SAX方式耗时：\t"
				+ (System.currentTimeMillis() - startTime) / NUMBER_OF_TEST
				+ "毫秒");

		startTime = System.currentTimeMillis();
		for (int i = 0; i < NUMBER_OF_TEST; i++) {
			newXMLByJDOM();
		}
		System.out.println("JDOM方式耗时：\t"
				+ (System.currentTimeMillis() - startTime) / NUMBER_OF_TEST
				+ "毫秒");

		startTime = System.currentTimeMillis();
		for (int i = 0; i < NUMBER_OF_TEST; i++) {
			newXMLByDOM4J();
		}
		System.out.println("DOM4J方式耗时：\t"
				+ (System.currentTimeMillis() - startTime) / NUMBER_OF_TEST
				+ "毫秒");
	}

	private void newXMLByDOM() throws ParserConfigurationException,
			TransformerFactoryConfigurationError, TransformerException {
		DocumentBuilder db = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		Document document = db.newDocument();
		document.setXmlStandalone(true);
		Element bookStore = document.createElement("bookstore");
		Element book = document.createElement("book");
		book.setAttribute("id", "1");
		Element name = document.createElement("name");
		name.setTextContent("信息简史");
		book.appendChild(name);
		bookStore.appendChild(book);
		document.appendChild(bookStore);
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		transformer.transform(new DOMSource(document), new StreamResult(
				new File("test_dom.xml")));
	}

	private void newXMLBySAX() throws TransformerConfigurationException,
			SAXException {
		SAXTransformerFactory stff = (SAXTransformerFactory) SAXTransformerFactory
				.newInstance();
		TransformerHandler handler = stff.newTransformerHandler();
		Transformer transformer = handler.getTransformer();
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		handler.setResult(new StreamResult(new File("test_sax.xml")));

		handler.startDocument();
		AttributesImpl attrs = new AttributesImpl();
		handler.startElement(null, null, "bookstore", attrs);
		attrs.addAttribute(null, null, "id", null, "1");
		handler.startElement(null, null, "book", attrs);
		attrs.clear();
		handler.startElement(null, null, "name", attrs);
		handler.characters("时间简史".toCharArray(), 0, "时间简史".length());
		handler.endElement(null, null, "name");
		handler.endElement(null, null, "book");
		handler.endElement(null, null, "bookstore");
		handler.endDocument();
	}

	private void newXMLByJDOM() throws FileNotFoundException, IOException {
		org.jdom2.Element bookStore = new org.jdom2.Element("bookstore");
		org.jdom2.Document document = new org.jdom2.Document(bookStore);
		org.jdom2.Element book = new org.jdom2.Element("book");
		book.setAttribute("id", "1");
		org.jdom2.Element name = new org.jdom2.Element("name");
		name.setText("时间简史");
		book.addContent(name);
		bookStore.addContent(book);

		Format format = Format.getPrettyFormat();
		format.setIndent("	");
		XMLOutputter outputter = new XMLOutputter(format);
		outputter.output(document, new FileOutputStream("test_jdom.xml"));
	}

	private void newXMLByDOM4J() throws IOException {
		org.dom4j.Document document = DocumentHelper.createDocument();
		org.dom4j.Element bookStore = document.addElement("bookstore");
		org.dom4j.Element book = bookStore.addElement("book");
		book.addAttribute("id", "1");
		org.dom4j.Element name = book.addElement("name");
		name.setText("时间简史");

		XMLWriter writer = new XMLWriter(
				new FileOutputStream("test_dom4j.xml"),
				OutputFormat.createPrettyPrint());
		writer.write(document);
		writer.close();
	}
}
